package com.photoeditor.demo.ui.widget.collage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.model.bean.BitmapBean;
import com.photoeditor.demo.model.image.collage.templet.CollagePoints;
import com.photoeditor.demo.model.image.collage.templet.attr.IStatus;
import com.photoeditor.demo.ui.widget.ScaleManager;
import com.photoeditor.demo.ui.widget.TextureVideoView;


/**
 * Created by winn on 17/9/11.
 */

public class CollageVideoView extends RelativeLayout implements ICollagePathView, TextureVideoView.MediaPlayerCallback {

    private final static float MAX_SCALE = 1.5f;

    //相对于父View的  View没有圆角和边距时的Rect
    private RectF mDefaultRect;

    //也是相对于父View的 当前View的Rect
    private RectF mCurrentRect;

    private CollagePoints mCollage;
    private IStatus mPreAttr;//预属性  用于镜像功能
    private Path mPath;
    private Region mRegion;
    private RectF mPathRect;

    private final static int BORDER_WIDTH = ComponentContext.getContext().getResources().getDimensionPixelSize(R.dimen.image_collage_border_width);
    private Paint mBorderPaint;
    private Paint mBitmapPaint;
    //绘制border的缩放值
    private float mBorderScaleX = 1.0f;
    private float mBorderScaleY = 1.0f;
    private float mBorderTranslateX = 0.0f;
    private float mBorderTranslateY = 0.0f;

    private float mTranslateX = 0.0f;
    private float mTranslateY = 0.0f;

    private boolean mIsTouch = false;
    private boolean mIsInChange = false;

    private BitmapBean mBitmapBean;
    private Bitmap mSourceBitmap;

    private TextureVideoView mVideoView;

    private boolean mIsScale = false;
    //初始化后的DisplayRect
    private RectF mDefaultDisplayRect;
    private RectF mDisplayRect;
    private Matrix mSupportMatrix;//第一次进入
    private Matrix mRotationMatrix;//旋转
    private Matrix mOppMatrix;//拉伸
    private Matrix mTranslateMatrix;//初始化平移
    private Matrix mDrawMatrix;//真正绘制
    private Matrix mCacheMatrix;//缓存

    private TextureVideoView.MediaPlayerCallback mMediaPlayerCallback;
    private int mId;

    public CollageVideoView(Context context, RectF rectF, Path path, RectF currentRect, RectF outSideRect, CollagePoints collage, IStatus preAttr) {
        super(context);
        init();
        mDefaultRect = rectF;
        mPathRect = new RectF();
        mRegion = new Region();
        mPath = path;
        mPath.computeBounds(mPathRect, false);
        //取交集部分
        mRegion.setPath(mPath, new Region((int) mPathRect.left, (int) mPathRect.top, (int) mPathRect.right, (int) mPathRect.bottom));
        mCollage = collage;
        mPreAttr = preAttr;
        setCurrentRect(currentRect, outSideRect);
    }

    public CollageVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CollageVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mVideoView = new TextureVideoView(getContext());
        mVideoView.setAutoAdjustSize(false);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(mVideoView, layoutParams);

        mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setColor(getResources().getColor(R.color.accent_color));
        mBorderPaint.setStrokeWidth(BORDER_WIDTH);
        mBorderPaint.setColor(getResources().getColor(R.color.collage_main_selected_color));

        mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmapPaint.setStyle(Paint.Style.FILL);
        mBitmapPaint.setColor(Color.WHITE);

        mSupportMatrix = new Matrix();
        mRotationMatrix = new Matrix();
        mOppMatrix = new Matrix();
        mTranslateMatrix = new Matrix();
        mCacheMatrix = new Matrix();
        mDrawMatrix = new Matrix();

        mDefaultDisplayRect = new RectF();
        mDisplayRect = new RectF();
        mCurrentRect = new RectF();
    }

    @Override
    public void translateWidthPreAttr(float dx, float dy) {
        float dst[] = getPreCountTranslate(dx, dy);
        mOppMatrix.postTranslate(dst[0], dst[1]);
        Matrix matrix = countDrawMatrix();
        RectF rectF = getDisplayRect(matrix);
        ensureNotInside(rectF);
        setDrawMatrix(countDrawMatrix());
    }

    @Override
    public float[] translateWidthoutPreAttr(float dx, float dy) {
        float dst[] = getPreCountTranslate(1, 1);
        mOppMatrix.postTranslate(dx, dy);
        Matrix matrix = countDrawMatrix();
        RectF rectF = getDisplayRect(matrix);
        ensureNotInside(rectF);
        setDrawMatrix(countDrawMatrix());
        return new float[]{dst[0] * dx, dst[1] * dy};
    }

    @Override
    public Region getRegion() {
        return mRegion;
    }

    @Override
    public void setIsTouch(boolean istouch) {
        if (mIsTouch != istouch) {
            mIsTouch = istouch;
            refresh();
        }
    }

    @Override
    public Path getPath() {
        return mPath;
    }

    @Override
    public void setData(RectF rectF, Path path, RectF currentRect, RectF outSideRect, CollagePoints collage, IStatus preAttr) {
        mDefaultRect = rectF;
        mPath = path;
        mPath.computeBounds(mPathRect, false);
        mRegion.setPath(mPath, new Region((int) mPathRect.left, (int) mPathRect.top, (int) mPathRect.right, (int) mPathRect.bottom));
        mCollage = collage;
        mPreAttr = preAttr;
        if (mBitmapBean != null) {
            reset();
            resetSupportMatrix();
            setCurrentRect(currentRect, outSideRect);
            setDrawMatrix(countDrawMatrix());
        } else {
            setCurrentRect(currentRect, outSideRect);
        }
    }

    @Override
    public void updateData(RectF rectF, Path path, RectF currentRect, RectF outSideRect, CollagePoints collage, IStatus preAttr) {
        mDefaultRect = rectF;
        mPath = path;
        mPath.computeBounds(mPathRect, false);
        mRegion.setPath(mPath, new Region((int) mPathRect.left, (int) mPathRect.top, (int) mPathRect.right, (int) mPathRect.bottom));
        mCollage = collage;
        mPreAttr = preAttr;
        if (mBitmapBean != null) {
            resetBaseMatrix();
            resetSupportMatrix();
            setCurrentRect(currentRect, outSideRect);
            setDrawMatrix(countDrawMatrix());
        } else {
            setCurrentRect(currentRect, outSideRect);
        }
    }

    private void resetBaseMatrix() {
        mSupportMatrix.reset();
        mCacheMatrix.reset();
        mDrawMatrix.reset();
    }

    @Override
    public void setIsInChange(boolean isInChange) {
        if (mIsInChange != isInChange) {
            mIsInChange = isInChange;
            refresh();
        }
    }

    @Override
    public boolean isInChange() {
        return mIsInChange;
    }

    @Override
    public void setSourceBitmap(Bitmap bitmap) {
        mSourceBitmap = bitmap;
    }

    @Override
    public void setBitmapBean(BitmapBean bitmapBean) {
        mBitmapBean = bitmapBean;
        reset();
        resetSupportMatrix();
        setDrawMatrix(countDrawMatrix());
    }

    @Override
    public Bitmap getCurrentBitmap(int alpha) {
        Bitmap bitmap = null;
        try {
            bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
//            Bitmap curBitmap = MediaThumbnailUtil.createPreViewVideoThumbnail(mBitmapBean.mPath, FULL_SCREEN_KIND, mVideoView.getCurrentPosition());
            Bitmap curBitmap = getSourceBitmap(0);
            Canvas canvas = new Canvas(bitmap);
            int count = canvas.saveLayerAlpha(0.0f, 0.0f, getWidth(), getHeight(), alpha, Canvas.ALL_SAVE_FLAG);

            Matrix matrix = getDrawMatrix();
            RectF rectF = getDisplayRect(matrix);
            ensureRect(rectF, matrix);
            canvas.drawBitmap(curBitmap, matrix, mBitmapPaint);

            canvas.restoreToCount(count);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    @Override
    public void setVideoPath(String string) {
        mVideoView.setVideoFilePath(string);
    }

    @Override
    public void onDestory() {
        mVideoView.stop();
    }

    @Override
    public void onResume() {
        resetSupportMatrix();
        setDrawMatrix(countDrawMatrix());
        mVideoView.resume();
    }

    @Override
    public void onPause() {
        mVideoView.pause();
    }

    @Override
    public void setCurrentRect(RectF currentRect, RectF outSideRect) {
        mCurrentRect.set(currentRect);
        mTranslateX = outSideRect.left - mCurrentRect.left;
        mTranslateY = outSideRect.top - mCurrentRect.top;
        mBorderScaleX = (mCurrentRect.width() - BORDER_WIDTH) / mCurrentRect.width();
        mBorderScaleY = (mCurrentRect.height() - BORDER_WIDTH) / mCurrentRect.height();
        mBorderTranslateX = mTranslateX + BORDER_WIDTH / 2;
        mBorderTranslateY = mTranslateY + BORDER_WIDTH / 2;

        mTranslateMatrix.reset();
//        mTranslateMatrix.postTranslate(mDefaultRect.left - mCurrentRect.left, mDefaultRect.top - mCurrentRect.top);
        Matrix matrix = countDrawMatrix();
        RectF rectF = getDisplayRect(matrix);
        setDrawMatrix(countDrawMatrix(), false);
    }

    @Override
    public CollagePoints getCollage() {
        return mCollage;
    }

    @Override
    public RectF getDefaultRect() {
        return mDefaultRect;
    }

    @Override
    public void setPath(Path path) {
        mPath.set(path);
        mPath.computeBounds(mPathRect, false);
        mRegion.setPath(mPath, new Region((int) mPathRect.left, (int) mPathRect.top, (int) mPathRect.right, (int) mPathRect.bottom));
    }

    @Override
    public void resetBound() {
        reset();
        resetSupportMatrix();
        setDrawMatrix(countDrawMatrix());
    }

    @Override
    public void refresh() {
        postInvalidate();
        mVideoView.invalidate();
    }

    public RectF getDisplayRect(Matrix matrix) {
        if (null != mBitmapBean) {
            mDisplayRect.set(0, 0, mBitmapBean.mWidth, mBitmapBean.mHeight);
            matrix.mapRect(mDisplayRect);
            return mDisplayRect;
        } else {
            mDisplayRect.set(0, 0, 0, 0);
        }
        return mDisplayRect;
    }

    @Override
    public float getTranslateX() {
        return mTranslateX;
    }

    @Override
    public float getTranslateY() {
        return mTranslateY;
    }

    @Override
    public Bitmap getSourceBitmap(long timestamp) {
        return mSourceBitmap;
    }

    @Override
    public RectF getCurrentRect() {
        return mCurrentRect;
    }

    @Override
    public boolean isNeedEnsureRect(float[] data) {
        RectF rectF = getDisplayRect(getDrawMatrix());
        RectF mViewCacheRect = new RectF();
        float dx = 0, dy = 0;
        if (rectF.height() > getHeight()) {//对上下边的处理
            if (rectF.top > 0f) {
                dy = -rectF.top;
            } else if (rectF.bottom < getHeight()) {
                dy = getHeight() - rectF.bottom;
            }
        } else {//高度比View高度小  需要居中
            mViewCacheRect.set(0, 0, getWidth(), getHeight());
            dy = (mViewCacheRect.height() - rectF.height()) / 2 + mViewCacheRect.top - rectF.top;
        }
        if (rectF.width() > getWidth()) {//对左右边的处理
            if (rectF.left > 0f) {
                dx = -rectF.left;
            } else if (rectF.right < getWidth()) {
                dx = getWidth() - rectF.right;
            }
        } else {//宽度比View高度小  需要居中
            mViewCacheRect.set(0, 0, getWidth(), getHeight());
            dx = (mViewCacheRect.width() - rectF.width()) / 2 + mViewCacheRect.left - rectF.left;
        }

        data[0] = -dx;
        data[1] = -dy;
        if (Math.abs(dx) > getWidth() / 5 || Math.abs(dy) > getHeight() / 5) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isTouch() {
        return mIsTouch;
    }

    @Override
    public BitmapBean getBitmapBean() {
        return mBitmapBean;
    }

    @Override
    public void setFilterBitmap(Bitmap bitmap) {
        mSourceBitmap = bitmap;
        if (mSourceBitmap != null && !mSourceBitmap.isRecycled()) {
            setDrawMatrix(countDrawMatrix());
        } else {
            reset();
        }
    }

    /**
     * 初始化SupportMatrix
     */
    private void resetSupportMatrix() {
        mSupportMatrix.reset();
        if (mBitmapBean == null) {
            return;
        }
        float bw = mBitmapBean.mWidth;
        float bh = mBitmapBean.mHeight;

        ScaleManager.Size viewSize = new ScaleManager.Size(mRegion.getBounds().width(), mRegion.getBounds().height());
        ScaleManager.Size videoSize = new ScaleManager.Size((int) bw, (int) bh);
        ScaleManager scaleManager = new ScaleManager(viewSize, videoSize);
        final Matrix matrix = scaleManager.getScaleMatrix(ScaleManager.ScaleType.CENTER_CROP);
        mSupportMatrix = matrix;

        mDrawMatrix.reset();
        float viewW = mRegion.getBounds().width();
        float viewH = mRegion.getBounds().height();
        float w, h;
        float scale;
        float translateX, translateY;
        if (bw / bh >= viewW / viewH) {//比较宽  这时候应高顶着高  才不会显示底图
            h = viewH;
            w = bw * viewH / bh;
            scale = w / bw;
            translateX = (viewW - w) / 2;
            translateY = 0.0f;
        } else {
            w = viewW;
            h = bh * viewW / bw;
            scale = w / bw;
            translateX = 0.0f;
            translateY = (viewH - h) / 2;
        }
        mDrawMatrix.postScale(scale, scale, 0, 0);
        mDrawMatrix.postTranslate(translateX, translateY);
        mDefaultDisplayRect.set(0, 0, bw, bh);
        //把坐标位置放入矩阵
        mDrawMatrix.mapRect(mDefaultDisplayRect);
        mDefaultDisplayRect.set(0, 0, bw, bh);
        //把坐标位置放入矩阵
        mDrawMatrix.mapRect(mDefaultDisplayRect);
    }

    /**
     * 计算当前的Draw Matrix
     *
     * @return
     */
    public Matrix countDrawMatrix() {
        mCacheMatrix.reset();
        mCacheMatrix.set(mSupportMatrix);
        mCacheMatrix.postConcat(mRotationMatrix);
        mCacheMatrix.postConcat(mOppMatrix);
        mCacheMatrix.postConcat(mTranslateMatrix);
        return mCacheMatrix;
    }

    /**
     * 获取Draw matrix
     *
     * @return
     */
    public Matrix getDrawMatrix() {
        mCacheMatrix.reset();
        mCacheMatrix.set(mDrawMatrix);
        mCacheMatrix.postConcat(mRotationMatrix);
        mCacheMatrix.postConcat(mOppMatrix);
        mCacheMatrix.postConcat(mTranslateMatrix);
        return mCacheMatrix;
    }

    public Matrix getOppMatrix() {
        return mOppMatrix;
    }

    /**
     * 设置Draw Matrix 并刷新
     *
     * @param matrix
     */
    public void setDrawMatrix(Matrix matrix) {
        mVideoView.setTransform(matrix);
        float[] data = new float[9];
        matrix.getValues(data);
        refresh();
    }

    /**
     * 设置Draw Matrix 并刷新
     *
     * @param matrix
     * @param needRefresh 是否需要刷新
     */
    public void setDrawMatrix(Matrix matrix, boolean needRefresh) {
        mVideoView.setTransform(matrix);
        float[] data = new float[9];
        matrix.getValues(data);
        if (needRefresh) {
            refresh();
        }
    }

    @Override
    public void flip(boolean horizontal) {

    }

    @Override
    public void rotation(int degree) {

    }

    @Override
    public boolean onSingleTapUp(float x, float y) {
        return false;
    }

    @Override
    public boolean onSingleTapComfirm(float x, float y) {
        return false;
    }

    @Override
    public boolean onDoubleTap(float x, float y) {
        return false;
    }

    @Override
    public boolean onScroll(float x, float y, float dx, float dy, float totalX, float totalY) {
        if (!mIsScale) {
            float translateX = -dx;
            float translateY = -dy;
            mOppMatrix.postTranslate(translateX, translateY);
            setDrawMatrix(countDrawMatrix());
        }
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return true;
    }

    @Override
    public boolean onScaleBegin(float focusX, float focusY) {
        mIsScale = true;
        return true;
    }

    @Override
    public boolean onScale(float focusX, float focusY, float scale) {
        if (Float.isNaN(scale) || Float.isInfinite(scale)) return false;
        Matrix drawMatrix = countDrawMatrix();
        RectF rect = getDisplayRect(drawMatrix);
        if (rect.width() * scale < getWidth() || rect.height() * scale < getHeight()) {
            drawMatrix.postScale(scale, scale, getWidth() / 2, getHeight() / 2);
            rect = getDisplayRect(drawMatrix);
            mOppMatrix.postScale(scale, scale, getWidth() / 2, getHeight() / 2);
            ensureRect(rect, null);
            // 控制放大比例
            float[] data = new float[9];
            mOppMatrix.getValues(data);
            if (data[0] > MAX_SCALE) {
                mOppMatrix.setScale(MAX_SCALE, MAX_SCALE, getWidth() / 2, getHeight() / 2);
            }
        } else {
            drawMatrix.postScale(scale, scale, focusX, focusY);
            rect = getDisplayRect(drawMatrix);
            mOppMatrix.postScale(scale, scale, focusX, focusY);
            ensureRect(rect, null);
            // 控制放大比例
            float[] data = new float[9];
            mOppMatrix.getValues(data);
            if (data[0] > MAX_SCALE) {
                mOppMatrix.setScale(MAX_SCALE, MAX_SCALE, focusX, focusY);
            }
        }

        setDrawMatrix(countDrawMatrix());
        return true;
    }

    @Override
    public void onScaleEnd() {
        mIsScale = false;
    }

    @Override
    public boolean onRotaionBegin() {
        return false;
    }

    @Override
    public boolean onRotaion(float delta) {
        return false;
    }

    @Override
    public void onRotaionEnd() {

    }

    @Override
    public void onDown(float x, float y) {
        mIsScale = false;
    }

    @Override
    public void onUp(float x, float y) {
//        if (!mVideoView.isPlaying() && mIsInChange) {
//            mVideoView.resume();
//        }
        mIsScale = false;
        RectF displayRect = getDisplayRect(getDrawMatrix());

        //判断需不需要复原
        if (displayRect.width() < getWidth() || displayRect.height() < getHeight()
                || displayRect.left > 0f || displayRect.right < getWidth()
                || displayRect.top > 0f || displayRect.bottom < getHeight()) {
            if (displayRect.width() < getWidth() || displayRect.height() < getHeight()) {//宽或者高比需要显示的小
                if (displayRect.width() < getWidth()) {
                    if (displayRect.left < 0) {
                        mOppMatrix.postTranslate(-displayRect.left, 0);
                    }
                    if (displayRect.right > getWidth()) {
                        mOppMatrix.postTranslate(getWidth() - displayRect.right, 0);
                    }
                } else {
                    if (displayRect.left > 0) {
                        mOppMatrix.postTranslate(-displayRect.left, 0);
                    }
                    if (displayRect.right < getWidth()) {
                        mOppMatrix.postTranslate(getWidth() - displayRect.right, 0);
                    }
                }
                if (displayRect.height() > getHeight()) {
                    if (displayRect.top > 0) {
                        mOppMatrix.postTranslate(0, -displayRect.top);
                    }
                    if (displayRect.bottom < getHeight()) {
                        mOppMatrix.postTranslate(0, getHeight() - displayRect.bottom);
                    }
                } else {
                    if (displayRect.top < 0) {
                        mOppMatrix.postTranslate(0, -displayRect.top);
                    }
                    if (displayRect.bottom > getHeight()) {
                        mOppMatrix.postTranslate(0, getHeight() - displayRect.bottom);
                    }
                }
                setDrawMatrix(countDrawMatrix());
            } else {//不需要复原只需要平移到合适位置
                if (displayRect.left > 0) {//左边
                    mOppMatrix.postTranslate(-displayRect.left, 0);
                }

                if (displayRect.top > 0) {
                    mOppMatrix.postTranslate(0, -displayRect.top);
                }

                if (displayRect.right < getWidth()) {
                    mOppMatrix.postTranslate(getWidth() - displayRect.right, 0);
                }

                if (displayRect.bottom < getHeight()) {
                    mOppMatrix.postTranslate(0, getHeight() - displayRect.bottom);
                }
                setDrawMatrix(countDrawMatrix());
            }
        }
    }


    @Override
    public void onLongPress(float x, float y) {
        mVideoView.pause();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        // 长按抓起view后，不显示播放view
        if (!mIsInChange) {
            if (!mVideoView.isPlaying()) {
                Bitmap bitmap = getSourceBitmap(0);
                if (bitmap != null) {
                    canvas.drawBitmap(bitmap, getDrawMatrix(), null);
                }
            }
            super.dispatchDraw(canvas);
        }
        //点击绘制显示边界
        if (mIsTouch) {
            int count3 = canvas.save();
            canvas.scale(mBorderScaleX, mBorderScaleY);
            canvas.translate(mBorderTranslateX, mBorderTranslateY);
            canvas.drawPath(mPath, mBorderPaint);
            canvas.restoreToCount(count3);
        }
    }

    /**
     * 计算合适的偏移量  当前显示Rect大于View的宽高时  保证不出边界
     * 当前显示Rect小于View的宽高时  保证居中
     *
     * @param rectF
     * @param matrix 传入Matrix的话就会对Matrix执行
     */
    private void ensureRect(RectF rectF, Matrix matrix) {

    }

    /**
     * 计算应用初始属性后的点
     *
     * @param dx
     * @param dy
     * @return
     */
    public float[] getPreCountTranslate(float dx, float dy) {
        if (mPreAttr != null) {
            float result[] = new float[2];
            int rotate = mPreAttr.getRotate();
            boolean flipH = mPreAttr.flipHorizontal();
            boolean flipV = mPreAttr.flipVertical();
            if (rotate != 0) {
                if ((rotate % 360 + 360) % 360 == 180) {
                    dx = -dx;
                    dy = -dy;
                }
            }
            if (flipH) {
                dx = -dx;
                dy = dy;
            }
            if (flipV) {
                dx = dx;
                dy = -dy;
            }
            result[0] = dx;
            result[1] = dy;
            return result;
        }
        return new float[]{dx, dy};
    }

    public void ensureNotInside(RectF rectF) {

    }

    /**
     * 重置所有的矩阵
     */
    public void reset() {
        mSupportMatrix.reset();
        mRotationMatrix.reset();
        mCacheMatrix.reset();
        mOppMatrix.reset();
        mDrawMatrix.reset();
        mTranslateMatrix.reset();
    }

    public boolean isMute() {
        return mVideoView.isMute();
    }

    public void mute(boolean mute) {
        if (mute) {
            mVideoView.mute();
        } else {
            mVideoView.unMute();
        }
    }

    public void start() {
        mVideoView.start();
    }

    public void resume() {
        mVideoView.resume();
    }

    public void seek2Begin() {
        mVideoView.seekTo(0);
    }

    public void pause() {
        mVideoView.pause();
    }

    public void stop() {
        mVideoView.stop();
    }

    public void setMediaPlayerCallback(TextureVideoView.MediaPlayerCallback mediaPlayerCallback, int id) {
        mMediaPlayerCallback = mediaPlayerCallback;
        mId = id;
        mVideoView.setMediaPlayerCallback(this, id);
    }

    @Override
    public void onPrepared(MediaPlayer mp, int id) {
        if (mMediaPlayerCallback != null) {
            mMediaPlayerCallback.onPrepared(mp, id);
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp, int id) {
        if (mMediaPlayerCallback != null) {
            mMediaPlayerCallback.onCompletion(mp, id);
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent, int id) {
        if (mMediaPlayerCallback != null) {
            mMediaPlayerCallback.onBufferingUpdate(mp, percent, id);
        }
    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height, int id) {
        if (mMediaPlayerCallback != null) {
            mMediaPlayerCallback.onVideoSizeChanged(mp, width, height, id);
        }
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra, int id) {

        if (mMediaPlayerCallback != null) {
            return mMediaPlayerCallback.onInfo(mp, what, extra, id);
        }
        return false;
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra, int id) {
        if (mMediaPlayerCallback != null) {
            return mMediaPlayerCallback.onError(mp, what, extra, id);
        }
        return false;
    }
}
